package com.dongjiantong.web.protal.controller.farmrecords;

/**
 * @author Lee
 * @date 2017/12/8
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.farmrecords.enums.FarmRecordsNumberStatus;
import com.dongjiantong.farmrecords.service.*;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.service.SysAreaService;
import com.dongjiantong.user.enums.FarmsEnum;
import com.dongjiantong.user.model.Farms;
import com.dongjiantong.user.model.UserArea;
import com.dongjiantong.user.service.FarmsService;
import com.dongjiantong.user.service.OrgUserAreaService;
import com.dongjiantong.user.service.RegionAccountService;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 养殖用户的养殖档案
 */
@Slf4j
@OpenBean
public class FarmRecordsController {
    @Reference(version = "1.0.0")
    public FarmsService farmsService;
    @Reference(version = "1.0.0")
    public OrgUserAreaService orgUserAreaService;

    @OpenMethod(key="web.selectFarmList",
           httpMethods = RequestMethod.GET)
    public String  tt(){

        return "zgsb";
    }
    /**
     * 根据条件查询养殖场List
     * @param pageSize
     * @param pageNum
     * @param animalType 养殖畜种
     * @param farmId 养殖场id
     * @param linkman   联系人(养殖场主)
     * @param areaList 区域编码list
     * @return
     */
    @OpenMethod(key = "web.farm.farmrecords.selectFarmList",
            paramNames = {"pageNum","pageSize","animalType","farmId","linkman","areaList"},
            httpMethods = RequestMethod.GET)
    public Response<PageInfo<Farms>> selectFarmList(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) List<Integer> animalType,
            @RequestParam(required = false) Long farmId,
            @RequestParam(required = false) String linkman,
            @RequestParam(required = false) List<Integer> areaList){
       /* if (userId == null){
            throw new OPServerException("用户未登录,请登陆后重试");
        }*/

       if (farmsService == null){
           throw new OPServerException("farmsService 服务null,请检查");
       }
       if (orgUserAreaService == null){
           throw new OPServerException("orgUserAreaService 服务null,请检查");
       }
        if (pageSize == null){
            pageSize = 20;
        }
        if (pageNum == null){
            pageNum = 1;
        }

        Map map = Maps.newHashMap();
        Long userId = UserUtil.getUserId();
        map.put("userId",userId);
        if (animalType.size() > 0){
            switch (animalType.size()){
                case 1:
                    map.put("animalOneType",animalType.get(0));
                    break;
                case 2:
                    map.put("animalTwoType",animalType.get(1));
                    break;
                default:
                    break;
            }
        }
        if (!Strings.isNullOrEmpty(linkman)){
            map.put("linkman","%" + linkman + "%");
        }

        map.put("farmStauts", FarmsEnum.status_true.getCode());
        List<Integer> farmTypeList = Lists.newArrayList(FarmsEnum.Breed.getCode(),FarmsEnum.Retail.getCode());
        map.put("farmsTypeList",farmTypeList);

        PageInfo<Farms> farmsPageInfo = null;
        if (farmId != null){
            //如果养殖场id不为空,则根据养殖场查询
            map.put("farmId",farmId);

            farmsPageInfo = farmsService.selectByUserIdPage(pageNum,pageSize,map);
            if (farmsPageInfo.getList().size() == 0){
                return Response.fail("查无数据");
            }
        }else {
            // 如果传入的地区code不为空
            if (areaList.size() > 0) {

                //获取当前登陆人的最高管辖级别
                int level = -1;
                List<UserArea> userAreaList = orgUserAreaService.queryByUserId(userId);
                boolean isProvince = userAreaList.stream().filter(city -> 0 == city.getCityId()).collect(Collectors.toList()).size() != 0;
                boolean isCity = userAreaList.stream().filter(region -> 0 == region.getRegionId()).collect(Collectors.toList()).size() != 0;
                boolean isRegion = userAreaList.stream().filter(street -> 0 == street.getStreetId()).collect(Collectors.toList()).size() != 0;
                boolean isStreet = userAreaList.stream().filter(village -> 0 == village.getVillageId()).collect(Collectors.toList()).size() != 0;
                if (isProvince) {
                    level = 1;//省级
                } else if (isCity) {
                    level = 2;//市级
                } else if (isRegion) {
                    level = 3;//县级
                } else if (isStreet) {
                    level = 4;//镇级
                } else {
                    level = 5;//村级
                }

                //判断传入数组的长度(传入数组长度与对应的管辖级别比对[省级对应的数组长度为1,市对应数组长度为2,县==>3,  镇==>4,  村==>5])
                int areaListSize = areaList.size();
                if (areaListSize < level) {
                    //查询范围超出管辖级别,查询登录人默认管辖的区域
                    //根据当前登录人id获取其管辖区域内的养殖场
                    farmsPageInfo = farmsService.selectByUserIdPage(pageNum,pageSize,map);
                    if (farmsPageInfo.getList().size() == 0){
                        return Response.fail("查无数据");
                    }
                } else {
                    //取出地区code,根据code查询其区域下的养殖场
                    int listSize = areaList.size();
                    switch (listSize){
                        case 1:
                            map.put("provinceId",areaList.get(0));
                            break;
                        case 2:
                            map.put("provinceId",areaList.get(0));
                            map.put("cityId",areaList.get(1));
                            break;
                        case 3:
                            map.put("provinceId",areaList.get(0));
                            map.put("cityId",areaList.get(1));
                            map.put("regionId",areaList.get(2));
                            break;
                        case 4:
                            map.put("provinceId",areaList.get(0));
                            map.put("cityId",areaList.get(1));
                            map.put("regionId",areaList.get(2));
                            map.put("streetId",areaList.get(3));
                            break;
                        case 5:
                            map.put("provinceId",areaList.get(0));
                            map.put("cityId",areaList.get(1));
                            map.put("regionId",areaList.get(2));
                            map.put("streetId",areaList.get(3));
                            map.put("villageId",areaList.get(4));
                            break;
                        default:
                            break;
                    }
                    //根据区域编号和当前登陆人查询其区域范围内的养殖场
                    farmsPageInfo = farmsService.selectByUserIdPage(pageNum,pageSize,map);
                    if (farmsPageInfo.getList().size() == 0){
                        return Response.fail("查无数据");
                    }
                }
            } else {

                //redis查找
            /*key = "FarmRecords:ObtainFarms.paramAreaCodeNull.userId=" + userId;
            List list1 = RedisUtil.getList(key);
            if (list1 != null && list1.size() > 0) {
                return Response.ok(list1);
            }*/

                //根据当前登录人id获取其管辖区域内的养殖场
                farmsPageInfo = farmsService.selectByUserIdPage(pageNum,pageSize,map);
                if (farmsPageInfo.getList().size() == 0){
                    return Response.fail("查无数据");
                }
            }
        }

        return Response.ok(farmsPageInfo);
    }
}
